﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Collision;
using JigLibX.Math;
using JigLibX.Vehicles;

namespace LloydGame
{
    public class JumpObject : GameObject
    {
        private Vector3 m_jumpVelocity;
        private float m_timeSinceLastEmittedStar = 0;

        public JumpObject(Game p_game, GameWorld p_gameWorld, String p_objectType, Vector3 p_position
                         , Vector3 p_rotation, Vector3 p_scale, bool p_staticFlag, String p_alias, Vector3 p_jumpVelocity)
            : base(p_game, p_gameWorld, p_objectType, p_position, p_rotation, p_scale, p_staticFlag, p_alias)
        {
            m_jumpVelocity = p_jumpVelocity;
            loadObjectType("GameContent\\Objects\\" + p_objectType + ".txt", p_gameWorld);
        }

        protected override bool collision_callbackFn(CollisionSkin skin0, CollisionSkin skin1)
        {
            GameObject obj0 = ((Game1)Game).gameWorld.GetObject(skin0.Owner);
            GameObject obj1 = ((Game1)Game).gameWorld.GetObject(skin1.Owner);

            if (obj0 == null || obj1 == null)
            {
                //Console.WriteLine("This should not happen! :S");
                return false;
            }

            if (obj0 == this && obj1.alias == "LocalCar")
            {
                ((Game1)Game).gameWorld.gamePlayer.carObject.body.Velocity = m_jumpVelocity; 
            }
            else if (obj1 == this && obj0.alias == "LocalCar")
            {
                ((Game1)Game).gameWorld.gamePlayer.carObject.body.Velocity = m_jumpVelocity;
            }

            return base.collision_callbackFn(skin0, skin1);
        }

        public override void Update(GameTime p_gameTime)
        {
            // emit stars
            m_timeSinceLastEmittedStar += p_gameTime.ElapsedGameTime.Milliseconds;
            if (m_timeSinceLastEmittedStar > 300)
            {
                //if (!((Game1)Game).gameWorld.PowerUpsToSpawn.Contains(this))
                    ((Game1)Game).gameWorld.starParticleSystem.spawnParticles(1, m_position, Vector3.Zero, null);

                m_timeSinceLastEmittedStar -= 300;
            }

            base.Update(p_gameTime);
        }
    }
}
